﻿using ContainerModule.ServiceReference1;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;

namespace ContainerModule.Views
{
    /// <summary>
    /// Interaction logic for AddModule6MainView.xaml
    /// </summary>
    [Export("AddModule6MainView")]
   
    public partial class AddModule6MainView : UserControl, IView
    {
        //ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:12951/odata"));
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));
        
        
        public AddModule6MainView()
        {
            //containersViewSource
            InitializeComponent();
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //여기에 데이터를 로드하고 결과를 CollectionViewSource에 할당하십시오.
                System.Windows.Data.CollectionViewSource containersViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["containersViewSource"];
                containersViewSource.Source = context.containers;
            }
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.container_nameTextBox.Text != null)
                {


                    container insertContainer = new container { container_name = this.container_nameTextBox.Text, container_decription = this.container_decriptionTextBox.Text };

                    //context.AddObject("Location_table", insertLocation);
                    context.AddTocontainers(insertContainer);

                    //context.BeginSaveChanges(AsynchCallback, insertLocation);
                    context.SaveChanges();

                    DataServiceCollection<container> lstData = new DataServiceCollection<container>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            this.containersDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.containers);

                    MessageBox.Show(container_nameTextBox.Text + "(이)가 입력되었습니다.", "입력완료 메세지");

                    this.container_decriptionTextBox.Text = "";
                    this.container_nameTextBox.Text = "";

                    //location_tableDataGrid.DataContext = context.Location_table;


                }
                else
                {
                    MessageBox.Show("값을 입력하시오");
                }
            }
            //catch (DataServiceRequestException dataServiceRequestException)
            //{
            //    MessageBox.Show("같은 값이 있네요~");
            //}
            catch (FormatException formatException)
            {
                MessageBox.Show("값을 입력하시오");
            }

        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.container_nameTextBox.Text != null)
            {
                var delete = this.container_nameTextBox.Text;

                //MessageBox.Show(delete);
                container selectItem = context.containers
                                            .Where(co => co.container_name == delete)
                                            .SingleOrDefault<container>();


                if (selectItem != null)
                {
                    context.DeleteObject(selectItem);
                    context.SaveChanges();


                    DataServiceCollection<container> lstData = new DataServiceCollection<container>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            this.containersDataGrid.ItemsSource = lstData;
                            
                        }
                    };

                    lstData.LoadAsync(context.containers);

                    MessageBox.Show(selectItem.container_name+ "(이)가 삭제되었습니다.", "삭제완료 메세지");

                    this.container_decriptionTextBox.Text = "";
                    this.container_nameTextBox.Text = "";

                }
                else
                {
                    MessageBox.Show("데이타가 없네요. 지울 데이터를 입력하시오");
                }
            }
            else
            {
                MessageBox.Show("지울 공장 data를 입력하시오");
                this.container_nameTextBox.Focus();
            }
        }

        private void SelectButton_Click(object sender, RoutedEventArgs e)
        {
            
            container cont = this.containersDataGrid.SelectedItem as container;

            this.container_nameTextBox.Text = cont.container_name;
            this.container_decriptionTextBox.Text = cont.container_decription;

            

        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.container_nameTextBox.Text != null)
                {
                    var update = new container{ container_name= this.container_nameTextBox.Text,
                        container_decription =this.container_decriptionTextBox.Text};

                    //MessageBox.Show(delete);
                    container selectItem = context.containers
                                                .Where(con => con.container_name == update.container_name)
                                                .SingleOrDefault<container>();

                    selectItem.container_decription = this.container_decriptionTextBox.Text;
                    


                    if (selectItem != null)
                    {
                        context.UpdateObject(selectItem);
                        context.SaveChanges();

                        DataServiceCollection<container> lstData = new DataServiceCollection<container>();
                        lstData.LoadCompleted += (senderobj, evtargs) =>
                        {
                            if (evtargs.Error == null)
                            {
                                this.containersDataGrid.ItemsSource = lstData;
                            }
                        };

                        lstData.LoadAsync(context.containers);


                        MessageBox.Show(selectItem.container_name+"의 수정이 완료 되었습니다.", "수정완료 메세지");
                        this.container_decriptionTextBox.Text = "";
                        this.container_nameTextBox.Text = "";

                    }
                }
            }
            catch (DataServiceQueryException cont)
            {
                MessageBox.Show("container_name은 변경이 불가능 합니다.");

                this.container_decriptionTextBox.Focus();
                
            }

        }

      
    }


    public class MultiConverter : IMultiValueConverter
    {

        
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return new container { container_decription = values[0].ToString(), container_name = values[1].ToString() };
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
